package com.cmcc.groupcontacts.firewall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.cmcc.groupcontacts.common.PreferenceManager;
import com.cmcc.groupcontacts.firewall.activity.strategy.WindowTitleStrategy;
import com.yinhai.activity.R;

/**
 * 拦截模式设置Activity
 * 
 * @author 
 * 
 */
public class FirewallConfigModeActivity extends BaseActivity {
	/** 智能拦截 */
	public static final int MODE_STANDARD = 0;
	/** 只拦截黑名单 */
	public static final int MODE_BLOCK_BLACK = 1;
	/** 只放行白名单 */
	public static final int MODE_PASS_WHITE = 2;
	/** 只放行本地通信录 */
	public static final int MODE_PASS_LOCAL_CONTACTS = 3;
	/** 只放行本地和集团通信录 */
	public static final int MODE_PASS_ALL_CONTACTS = 4;
	/** 拦截全部 */
	public static final int MODE_BLOCK_ALL = 5;
	/** 自定义拦截规则 */
	public static final int MODE_CUSTOMIZE = 6;

	@Override
	public void setTitleStrategy() {
		titleStrategy = new WindowTitleStrategy(this);
	}

	@Override
	public void setTitleButtons() {
		// 左边的“返回”按钮
		getLeftLayout().addView(createBackButton());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firewall_config_mode_activity);

		init();
	}

	private void init() {
		setSelectedItem(PreferenceManager.getFirewallMode());
		setOnClickListener(findViewById(R.id.lay_mode_standard), MODE_STANDARD);
		setOnClickListener(findViewById(R.id.lay_mode_block_black), MODE_BLOCK_BLACK);
		setOnClickListener(findViewById(R.id.lay_mode_pass_white), MODE_PASS_WHITE);
		setOnClickListener(findViewById(R.id.lay_mode_pass_local_contacts), MODE_PASS_LOCAL_CONTACTS);
		setOnClickListener(findViewById(R.id.lay_mode_pass_all_contacts), MODE_PASS_ALL_CONTACTS);
		setOnClickListener(findViewById(R.id.lay_mode_block_all), MODE_BLOCK_ALL);
		setOnClickListener(findViewById(R.id.lay_mode_customize), MODE_CUSTOMIZE);
	}

	private void setOnClickListener(View v, final int order) {
		v.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedItem(order);
				PreferenceManager.setFirewallMode(order);
			}
		});
	}

	/*
	 * 设置单选按钮的选中项
	 */
	private void setSelectedItem(int order) {
		RadioButton radio0 = ((RadioButton) findViewById(R.id.radio_mode_standard));
		RadioButton radio1 = ((RadioButton) findViewById(R.id.radio_mode_block_black));
		RadioButton radio2 = ((RadioButton) findViewById(R.id.radio_mode_pass_white));
		RadioButton radio3 = ((RadioButton) findViewById(R.id.radio_mode_pass_local_contacts));
		RadioButton radio4 = ((RadioButton) findViewById(R.id.radio_mode_pass_all_contacts));
		RadioButton radio5 = ((RadioButton) findViewById(R.id.radio_mode_block_all));
		RadioButton radio6 = ((RadioButton) findViewById(R.id.radio_mode_customize));
		View layout6 = findViewById(R.id.lay_mode_customize_edit);
		TextView textview6 = (TextView) findViewById(R.id.txt_mode_customize_edit);
		radio0.setChecked(false);
		radio1.setChecked(false);
		radio2.setChecked(false);
		radio3.setChecked(false);
		radio4.setChecked(false);
		radio5.setChecked(false);
		radio6.setChecked(false);
		layout6.setEnabled(false);
		layout6.setOnClickListener(null);
		textview6.setTextColor(getColor(R.color.list_item_phone));
		switch (order) {
		case MODE_STANDARD:
			radio0.setChecked(true);
			break;
		case MODE_BLOCK_BLACK:
			radio1.setChecked(true);
			break;
		case MODE_PASS_WHITE:
			radio2.setChecked(true);
			break;
		case MODE_PASS_LOCAL_CONTACTS:
			radio3.setChecked(true);
			break;
		case MODE_PASS_ALL_CONTACTS:
			radio4.setChecked(true);
			break;
		case MODE_BLOCK_ALL:
			radio5.setChecked(true);
			break;
		case MODE_CUSTOMIZE:
			radio6.setChecked(true);
			layout6.setEnabled(true);
			layout6.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivity(new Intent(FirewallConfigModeActivity.this, FirewallConfigModeCustomActivity.class));
				}
			});
			textview6.setTextColor(getColor(R.color.list_item_name));
			break;
		}
	}
}
